package com.msc.mobilecomputing;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.widget.Toast;

import com.msc.mobilecomputing.fragments.DescriptionFragment;
import com.msc.mobilecomputing.fragments.DirectionsFragment;
import com.msc.mobilecomputing.model.TravelLocation;

public class DescriptionActivity extends FragmentActivity {

	public static final String INTENT_EXTRA_LOCATION = "location";

	private TravelLocation travelLocation;

	private ViewPager viewPager;

	private DescriptionPagerAdapter pagerAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_description);

		getActionBar().setDisplayHomeAsUpEnabled(true);

		if (getIntent().hasExtra(INTENT_EXTRA_LOCATION)) {
			travelLocation = (TravelLocation) getIntent().getSerializableExtra(
					INTENT_EXTRA_LOCATION);
			initUI();
		} else {
			Toast.makeText(this, "Error occured", Toast.LENGTH_LONG).show();
			finish();
		}
	}

	private void initUI() {

		viewPager = (ViewPager) findViewById(R.id.pager);
		pagerAdapter = new DescriptionPagerAdapter(getSupportFragmentManager());
		viewPager.setAdapter(pagerAdapter);
		viewPager
				.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
					@Override
					public void onPageSelected(int position) {
						getActionBar().setSelectedNavigationItem(position);
					}
				});

		// Specify that tabs should be displayed in the action bar.
		ActionBar actionBar = getActionBar();

		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		// Create a tab listener that is called when the user changes tabs.
		ActionBar.TabListener tabListener = new ActionBar.TabListener() {
			@Override
			public void onTabReselected(Tab tab,
					android.app.FragmentTransaction ft) {

			}

			@Override
			public void onTabSelected(Tab tab,
					android.app.FragmentTransaction ft) {
				viewPager.setCurrentItem(tab.getPosition());

			}

			@Override
			public void onTabUnselected(Tab tab,
					android.app.FragmentTransaction ft) {
			}
		};

		for (int i = 0; i < 2; i++) {
			actionBar.addTab(actionBar.newTab()
					.setText(pagerAdapter.getPageTitle(i))
					.setTabListener(tabListener));
		}

	}

	public TravelLocation getTravelLocation() {
		return travelLocation;
	}

	public void setTravelLocation(TravelLocation travelLocation) {
		this.travelLocation = travelLocation;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private class DescriptionPagerAdapter extends FragmentPagerAdapter {
		public DescriptionPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int i) {
			Fragment fragment = null;
			if (i == 0) {
				fragment = new DescriptionFragment();
				Bundle args = new Bundle();
				args.putSerializable(DescriptionFragment.EXTRA,
						getTravelLocation());
				fragment.setArguments(args);
			} else if (i == 1) {
				fragment = new DirectionsFragment();
				Bundle args = new Bundle();
				args.putSerializable(DirectionsFragment.EXTRA,
						getTravelLocation());
				fragment.setArguments(args);
			}
			return fragment;
		}

		@Override
		public int getCount() {
			return 2;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			if (position == 0) {
				return "Description";
			} else {
				return "Direction";
			}
		}
	}

}
